<?php

namespace App\Models;

use App\Enums\DomainType;
use App\Models\Traits\BelongsToApplication;
use App\Models\Traits\HasCovers;
use App\Models\Traits\HasEasyStatus;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;

class Season extends Model
{
    use BelongsToApplication,
        Cachable,
        HasEasyStatus,
        HasCovers,
        SoftDeletes;

    protected $casts = [
        'config' => 'json',
    ];

    public function wechat(): BelongsTo
    {
        return $this->belongsTo(Wechat::class);
    }

    public function gifts(): HasMany
    {
        return $this->hasMany(Gift::class);
    }

    public function sponsors(): HasMany
    {
        return $this->hasMany(Sponsor::class);
    }

    public function chapters(): HasMany
    {
        return $this->hasMany(Chapter::class);
    }

    public function players(): HasMany
    {
        return $this->hasMany(Player::class);
    }

    public function supports(): HasMany
    {
        return $this->hasMany(Support::class);
    }

    public function designs(): HasMany
    {
        return $this->hasMany(Design::class);
    }

    public function getAudioUrlAttribute(): string
    {
        return $this->parseImageUrl($this->audio);
    }

    public function getConfig(string|array $key = null)
    {
        $list = [
            'allow_custom_amount' => (bool) $this->config['allow_custom_amount'],
            'amount_max' => (int) $this->config['amount_max'],
            'amount_min' => (int) $this->config['amount_min'],
            'amount_tips' => (int) $this->config['amount_tips'],
            'captcha_type' => $this->config['captcha_type'],
            'comment_period' => (int) $this->config['comment_period'],
            'comment_period_times' => (int) $this->config['comment_period_times'],
            'comment_polls' => (int) $this->config['comment_polls'],
            'first_show_before' => (int) $this->config['first_show_before'],
            'like_period' => (int) $this->config['like_period'],
            'like_period_times' => (int) $this->config['like_period_times'],
            'like_polls' => (int) $this->config['like_polls'],
            'rank_lowest_polls' => (int) $this->config['rank_lowest_polls'],
            'robot_day_max' => (int) $this->config['robot_day_max'],
            'robot_day_max_ip' => (int) $this->config['robot_day_max_ip'],
            'show_captcha' => (bool) $this->config['show_captcha'],
            'show_comment' => (bool) $this->config['show_comment'],
            'show_comment_log' => (bool) $this->config['show_comment_log'],
            'show_like' => (bool) $this->config['show_like'],
            'show_like_log' => (bool) $this->config['show_like_log'],
            'show_rank_time' => (int) $this->config['show_rank_time'],
            'show_support' => (bool) $this->config['show_support'],
            'show_support_log' => (bool) $this->config['show_support_log'],
            'support_polls' => (int) $this->config['support_polls'],
            'time_check_type' => $this->config['time_check_type'],
            'winners' => (int) $this->config['winners'],
        ];

        if (is_string($key)) {
            return $list[$key] ?? null;
        }

        if (is_array($key)) {
            return Arr::only($list, $key);
        }

        return $list;
    }

    public function getMiddleDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::MIDDLE)->exists()) {
            return $this->spider()->where('type', DomainType::MIDDLE)->first()->domainPool;
        } else {
            return null;
        }
    }

    public function spider(): MorphOne
    {
        return $this->morphOne(Spider::class, 'spiderable');
    }

    public function getPlayerDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::PLAYER)->exists()) {
            return $this->spider()->where('type', DomainType::PLAYER)->first()->domainPool;
        } else {
            return null;
        }
    }

    public function getRankDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::RANK)->exists()) {
            return $this->spider()->where('type', DomainType::RANK)->first()->domainPool;
        } else {
            return null;
        }
    }

    public function getType(): string
    {
        return '<span class="label" style="background:#586cb1">赛季</span>';
    }

    public function getTitle()
    {
        return $this->title;
    }
}
